
import math
import uuid

# category no 的单层code长度
CATEGORY_NO_LEVEL_LENGTH = 4

class PageRequest:
    def __init__(self,page = 1,size = 20):
        self.page = page
        self.size = size

    def take(self):
        return (self.page - 1) * self.size

class PageResponse:
    def __init__(self,data,total,page,size):
        self.data = data
        self.total = total
        self.page = page
        self.size = size
        self.total_page = math.ceil(total / size)



def not_blank(val):
    if val is None:
        return False

    if type(val) == 'str':
        return len(val) > 0
    else:
        return len(val[0]) > 0


def fetch2Json(data,description):
    result = []
    if(len(data) <=0):
        return result
    else:
        for i in data:
            item = {}
            for index in range(len(description)):
                item[description[index][0]] = i[index]

            result.append(item)
    return result

def randomFileName(file_name):
    return uuid.uuid3(uuid.NAMESPACE_DNS,file_name).__str__() + '.' + fileType(file_name)

def fileType(file_name):
    return file_name.rsplit('.', 1)[1].lower()


class LoginUser(object):
    def __init__(self,id,name,avatar):
        self.id = id
        self.username = name
        self.avatar = avatar
        self.token=id
loginStorage = {}

def checkWeapp(token):
    return token in loginStorage

def currentUser(token):
    return loginStorage[token]

def loginRefresh(user:LoginUser):
    loginStorage[str(user.id)] = user
